using System.Collections.Generic;
using System.IO;
using Gameplay.PVE.Editor;
using UltimateGameTools;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using Yoozoo.Gameplay.RTS;

namespace GameFramework.Battle.Tools
{
    [CustomEditor(typeof(BoneBinder)),CanEditMultipleObjects]
    public class BoneBinderEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUILayout.Button("导出骨骼文件"))
            {
                Export(target as BoneBinder);
            }
        }
        
        public void Export(BoneBinder bb)
        {
            var animator = bb.gameObject.GetComponent<Animator>();
            if (animator == null)
            {
                EditorUtility.DisplayDialog("通知", "没有找到Animator", "wssb");
                return;
            }
            Dictionary<string,float> dic = new Dictionary<string,float>();
            //AnimatorControllerLayer layer = ((AnimatorController)animator.runtimeAnimatorController).layers[0];
            var clips = animator.runtimeAnimatorController.animationClips;
            for (int i = 0; i < clips.Length; i++)
            {
                var clip = clips[i];
                dic.Add(clip.name,clip.length);
                /*var animDic = new Dictionary<string, List<float>>();
                dic.Add(clip.name,animDic);
                foreach (var binding in AnimationUtility.GetCurveBindings(clip))
                {
                    var list = new List<float>();
                    animDic.Add(binding.path,list);
                    AnimationCurve curve = AnimationUtility.GetEditorCurve(clip, binding);
                    
                    //EditorGUILayout.LabelField(binding.path + "/" + binding.propertyName + ", Keys: " + curve.keys.Length);
                    for (int j = 0; j < curve.length; j++)
                    {
                        Debug.LogError(curve[i].time+ " " + curve[j].value);
                    }
                }*/
            }

            var editorAnimatorPlayer = new EditorAnimPlayer();
            editorAnimatorPlayer.Initialize(animator);

            string path = "Assets/ResourcesAssets/Config/RTSScriptConfig/" + bb.gameObject.name + ".asset";
            GPUSkinning_BoneNode data;
            if (File.Exists(path))
            {
                data = AssetDatabase.LoadAssetAtPath<GPUSkinning_BoneNode>(path);
            }
            else
            {
                data = ScriptableObject.CreateInstance<GPUSkinning_BoneNode>();
                AssetDatabase.CreateAsset(data, path);
            }
            data.anims = new List<GPUSkinningAnim>();
            foreach (var item in dic)
            {
                GPUSkinningAnim anim = new GPUSkinningAnim();
                anim.animName = item.Key;
                anim.bones = new List<GPUSkinningBone>();
                data.anims.Add(anim);
                editorAnimatorPlayer.Play(item.Key);//播放每个动作，记录骨骼上的点位
                Dictionary<Transform, GPUSkinningBone> bones = new Dictionary<Transform, GPUSkinningBone>();
                int frame = 0;
                for (float time = 0; time < item.Value; time += 1 / 60f)
                {
                    editorAnimatorPlayer.Update(time);
                    for (int i = 0; i < bb.boneNodes.Count; i++)
                    {
                        for (int j = 0; j < bb.boneNodes[i].bones.Count; j++)
                        {
                            var transform = bb.boneNodes[i].bones[j];
                            if (!bones.ContainsKey(transform))
                            {
                                var bone = new GPUSkinningBone();
                                bone.boneName = transform.name;
                                bone.boneType = bb.boneNodes[i].type;
                                bone.frames = new List<GPUSkinningAnimFrame>();
                                bones.Add(transform,bone);
                            }
                            bones[transform].frames.Add(new GPUSkinningAnimFrame
                            {
                                time = time,
                                frame = frame,
                                position = bb.boneNodes[i].bones[j].position,
                            });
                        }
                    }
                    frame++;
                }

                foreach (var item2 in bones.Values)
                {
                    anim.bones.Add(item2);
                }
            }
            
            EditorUtility.SetDirty(data);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.DisplayDialog("通知", "导出成功", "OK");
        }
    }
}